package com.itheima.appserver.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.appserver.exception.BusinessException;
import com.itheima.appserver.interceptor.UserHandler;
import com.itheima.autoconfig.template.HuanXinTemplate;
import com.itheima.dubbo.api.*;
import com.itheima.model.domain.Announcement;
import com.itheima.model.domain.User;
import com.itheima.model.domain.UserInfo;
import com.itheima.model.enums.CommentType;
import com.itheima.model.mongo.Comment;
import com.itheima.model.mongo.Friend;
import com.itheima.model.vo.*;
import com.itheima.utils.Constants;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private FriendApi friendApi;
    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private MovementApi movementApi;
    @DubboReference
    private AnnouncementApi announcementApi;

    public UserInfoVo findHuanXinuser(String hunaxinId) {
        //根据huanxinid查询用户
        User user = userApi.findhuanxin(hunaxinId);
        //根据用户id拆线呢用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());
        //创建返回vo对象
        UserInfoVo vo = new UserInfoVo();
        //copy同类名的属性
        BeanUtils.copyProperties(userInfo, vo);
        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    //添加好友
    public void contacts(Long friendId) {
        //获取当前操作用户id
        Long userId = UserHandler.getId();
        //将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userId, Constants.HX_USER_PREFIX + friendId);
        //判断是否添加成功
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        //添加成功调用friendapi绑定好友关系
        friendApi.save(userId, friendId);
    }

    //删除好友
    public void uncontacts(Long friendId) {
        //获取当前操作用户id
        Long userId = UserHandler.getId();
        //将环信好友关系删除
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + userId, Constants.HX_USER_PREFIX + friendId);
        //判断是否添加成功
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        //添加成功调用friendapi绑定好友关系
        friendApi.delete(userId, friendId);
    }

    public PageResult findByFriend(Integer page, Integer pagesize, String keyword) {
        //调用api查询用户好友
        List<Friend> list = friendApi.findByFriend(UserHandler.getId(), page, pagesize);
        //判断是否为空
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //获取好友id
        List<Long> friendIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //调用userinfo查询用户详情信息
        //构造用户条件对象
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(friendIds, userInfo);
        //创建返回vo对象
        List<ContactVo> vos = new ArrayList<>();
        //遍历list
        for (Friend friend : list) {
            UserInfo info = map.get(friend.getFriendId());
            if (info != null) {
                ContactVo vo = ContactVo.init(info);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0, vos);
    }

    public PageResult findUservo(Integer page, Integer pagesize, CommentType commentType) {
        //获取用户id
        Long userId = UserHandler.getId();
        //调用api查询
        List<Comment> items = commentApi.findUserVo(page, pagesize, userId, commentType);
        //判断是否为空
        if (CollUtil.isEmpty(items)) {
            return new PageResult();
        }
        //不为空获取用户id
        List<Long> ids = CollUtil.getFieldValues(items, "userId", Long.class);
        //调用userinfoapi查询用户详情
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids, null);
        //创建vo返回对象
        List<CommentVo> vos = new ArrayList<>();
        //遍历items
        for (Comment item : items) {
            UserInfo userInfo = map.get(item.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, item);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0, vos);
    }

    public PageResult announcements(Integer page, Integer pagesize) {
        //调用api查询
        Page<Announcement> announcements = announcementApi.announcements(page, pagesize);
        //判断是否为空
        List<Announcement> records = announcements.getRecords();
        if (CollUtil.isEmpty(records)) {
            return new PageResult();
        }
        //不为空
        //创建返回vo集合对象
        List<AnnouncementVo> vos = new ArrayList<>();
        //遍历list
        for (Announcement announcement : records) {
            if (announcement != null) {
                AnnouncementVo vo = AnnouncementVo.init(announcement);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0, vos);
    }
}
